package com.admin.common.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 *
 * @author NL
 */
public class DateUtils {

    /**
     * 日期格式化
     */
    public final static SimpleDateFormat sd = new SimpleDateFormat("yy");
    public final static SimpleDateFormat sdfy = new SimpleDateFormat("yyyy");
    public final static SimpleDateFormat sdfym = new SimpleDateFormat("yyyy-MM");
    public final static SimpleDateFormat sdfymd = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat sdfymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static SimpleDateFormat sdfymdhm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public final static SimpleDateFormat sdfymdhms2 = new SimpleDateFormat("yyyyMMddHHmmss");
    public final static SimpleDateFormat sdfymdhms3 = new SimpleDateFormat("yyyyMMdd");
    public final static SimpleDateFormat sdfymd2 = new SimpleDateFormat("yyyy.MM.dd");
    public final static SimpleDateFormat sdfymdh4 = new SimpleDateFormat("yyyyMMddHH");
    public final static SimpleDateFormat sdfmdh = new SimpleDateFormat("MM/dd HH:mm");
    public final static SimpleDateFormat sdfhms = new SimpleDateFormat("MM/dd HH:mm:ss");
    public final static SimpleDateFormat sdfymdf = new SimpleDateFormat("yyyy/MM/dd");
    public static final String DATE_YEAR_MONTH_DAY = "yyyy年MM月dd日";

    // 30天
    public static final int THIRTY_DAY = 2592000;
    // 1小时
    public static final int ONR_HOUSE = 3600;

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";

    public final static String DATE_PATTERN_YEAR_MONTH = "yyyy-MM";
    /**
     * 时间格式(yyyy-MM-dd HH:mm)
     */
    public final static String DATE_PATTERN_TIME = "yyyy-MM-dd HH:mm";

    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_PATTERN_SS_TIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyyMMddHHmmss";

    public final static String DATE_TIME_PATTERN_YMD = "yyyyMMdd";

    public static String dateFormatStr(Integer longDate) {
        if (null == longDate) {
            return "";
        }
        Date date = new Date();
        date.setTime(longDate * 1000L);
        return DateUtils.sdfymdhms.format(date);
    }

    public static String dateFormatYmdStr(Integer longDate) {
        if (null == longDate) {
            return "";
        }
        Date date = new Date();
        date.setTime(longDate * 1000L);
        return DateUtils.sdfymd.format(date);
    }

    public static String dateFormatYmdHmStr(Integer longDate) {
        if (null == longDate) {
            return "";
        }
        Date date = new Date();
        date.setTime(longDate * 1000L);
        return DateUtils.sdfymdhm.format(date);
    }


    public static String dateFormatStrToMin(Integer longDate) {
        Date date = new Date();
        date.setTime(longDate * 1000L);
        return DateUtils.sdfymdf.format(date);
    }

    public static String dateFormatStr(Integer longDate, String pattern) {
        Date date = new Date();
        date.setTime(longDate * 1000L);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前秒的时间戳
     *
     * @return
     */
    public static Integer getTimeStamp() {
        Long time = Calendar.getInstance().getTimeInMillis() / 1000;
        return time.intValue();
    }
    /**
     * 1年后的头一天
     *
     * @return
     * @throws Exception
     */
    public static Integer getYearBefore() {
        Date date = new Date();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);//当前时间加去一年，即一年前的时间
        calendar.add(Calendar.DATE, -1);//当前时间前去一个月，即一个月前的时间
        Long time=calendar.getTimeInMillis()/1000;
        return time.intValue();//获取一年前的时间，或者一个月前的时间
    }



    /**
     * 获取指定时间的时间戳
     * 
     * @param date
     * @return
     */
    public static Integer getTimeStamp(Date date) {
        Long time = date.getTime() / 1000;
        return time.intValue();
    }

    /**
     * 获取下一个整点剩余秒
     *
     * @return
     */
    public static long getTheNextWholeTimeStamp() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR_OF_DAY, 1);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long time = cal.getTimeInMillis() / 1000 - Calendar.getInstance().getTimeInMillis() / 1000;
        return time;
    }

    /**
     * 获取明天0点剩余秒
     *
     * @return
     */
    public static long getTheRemainingSecondsOfTomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long time = cal.getTimeInMillis() / 1000 - Calendar.getInstance().getTimeInMillis() / 1000;
        return time;
    }

    /**
     * 获取日期示例
     */
    public static void DateAcquisition() {
        System.out.println("-----时间获取-----");
        Calendar cal = Calendar.getInstance();
        System.out.println("获取当前时间:" + sdfymdhms.format(cal.getTime()));
        System.out.println("获取当前时间戳:" + cal.getTimeInMillis());
        System.out.println("获取当前是一年中第几天:" + cal.get(Calendar.DAY_OF_YEAR));
        System.out.println("获取当前年:" + cal.get(Calendar.YEAR));
        System.out.println("获取当前月:" + (cal.get(Calendar.MONTH) + 1));
        System.out.println("获取当前日:" + cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("获取当前周:" + (cal.get(Calendar.DAY_OF_WEEK) - 1));
        System.out.println("获取当前时:" + cal.get(Calendar.HOUR_OF_DAY));
        System.out.println("获取当前分:" + cal.get(Calendar.MINUTE));
        System.out.println("获取当前秒:" + cal.get(Calendar.SECOND));
        System.out.println("获取当前毫秒:" + cal.get(Calendar.MILLISECOND));


    }

    public static int getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MONTH) + 1;
    }

    public static Integer getCurrentYear() {
        Calendar cal = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();

        calendar.clear();

        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));

        Date currYearFirst = calendar.getTime();

        Long time = currYearFirst.getTime()/1000;
        return time.intValue() -1;
    }


    public static void main(String[] args) {

        System.out.println(getCurrentYear());

        /*Calendar cal = Calendar.getInstance();



        Calendar calendar = Calendar.getInstance();

        calendar.clear();

        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));

        Date currYearFirst = calendar.getTime();

        System.out.println(currYearFirst.getTime()/1000);*/


    }


    /**
     * 获取当前周数
     */
    public static int dayOfWeek() {
        Calendar cal = Calendar.getInstance();
        int tmp = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (0 == tmp) {
            tmp = 7;
        }
        return tmp;
    }

    /**
     * 获取当前小时数
     */
    public static int hourOfDay() {
        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 时间加减操作
     */
    public static void DateAdditionAndSubtraction() {
        Calendar cal = Calendar.getInstance();
        System.out.println("-----时间加减-----");
        System.out.println("时间加减方法:cal.addAddress(Calendar.函数, 负数/正数);/n例如:");
        cal.add(Calendar.YEAR, 1);
        System.out.println("加一年后的日期:" + sdfymdhms.format(cal.getTime()));
        cal.add(Calendar.YEAR, -1);
        System.out.println("减一年后的日期:" + sdfymdhms.format(cal.getTime()));
        cal.add(Calendar.MONTH, 1);
        System.out.println("加一月后的日期:" + sdfymdhms.format(cal.getTime()));
        cal.add(Calendar.MONTH, -1);
        System.out.println("减一月后的日期:" + sdfymdhms.format(cal.getTime()));
        // 加指定天数
        cal.setTimeInMillis(1542889039000L);
        cal.add(Calendar.DAY_OF_MONTH, 2);
        System.out.println("加2天后的日期:" + sdfymdhms.format(cal.getTime()));
    }

    /**
     * @Function 时间加减操作
     * @author likaixuan
     * @Date 2018-11-22 20:29
     * @return void
     */
    public static int dateAdditionAndSubtraction(int intTime, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(intTime * 1000L);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return (int)(cal.getTimeInMillis() / 1000);
    }

    /**
     * 时间加月份
     * 
     * @param intTime
     * @param month
     * @return
     */
    public static int addMonth(int intTime, int month) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTimeInMillis(intTime * 1000L);
        cal.add(Calendar.MONTH, month);
        return (int)(cal.getTimeInMillis() / 1000);
    }

    /**
     * 时间加月份
     *
     * @param intTime
     * @param month
     * @return
     */
    public static int addMonthbefore(int intTime, int month) {
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTimeInMillis(intTime * 1000L);
        cal.add(Calendar.MONTH, month);
        cal.add(Calendar.DATE, -1);//当前时间前去一个月，即一个月前的时间
        return (int)(cal.getTimeInMillis() / 1000);
    }
    /**
     * 获取某(年/月/周/日)最小或最大日期
     *
     * @param cal 日期:如果为当前填写:Calendar.getInstance()
     * @param ymwd 填写(y:年;m:月;w:周;d:日)
     * @param size 填写(0:最小;1:最大)
     * @return
     */
    public static Date dayMaxTime(Calendar cal, String ymwd, int size) {
        if ("y".equals(ymwd)) {
            if (size == 0) {
                cal.set(Calendar.MONTH, 0);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            } else {
                cal.set(Calendar.MONTH, 11);
                cal.set(Calendar.DAY_OF_MONTH, 31);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
            }
        } else if ("m".equals(ymwd)) {
            if (size == 0) {
                cal.set(Calendar.DAY_OF_MONTH, 1);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            } else {
                cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
            }
        } else if ("w".equals(ymwd)) {
            if (size == 0) {
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            } else {
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                cal.add(Calendar.DAY_OF_MONTH, 6);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
            }
        } else if ("d".equals(ymwd)) {
            if (size == 0) {
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
            } else {
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
            }
        }
        return cal.getTime();
    }

    /**
     * 当月第一天 秒
     * 
     * @return
     * @throws Exception
     */
    public static Long getMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        // 设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 次月第一天 秒
     * 
     * @return
     * @throws Exception
     */
    public static Long getNextMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        // 设置为1号,当前日期既为当月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        // 日期加一个月
        c.add(Calendar.MONTH, 1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 当月最后一天 秒
     * 
     * @return
     * @throws ParseException
     */
    public static Long getMonthLast() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        // 设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        // 将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至59
        c.set(Calendar.MINUTE, 59);
        // 将秒至59
        c.set(Calendar.SECOND, 59);
        // 将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 上月第一天 秒
     * 
     * @return
     * @throws Exception
     */
    public static Long getPrevMonthFirst() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        // 设置为1号,当前日期既为上月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        // 日期减一个月
        c.add(Calendar.MONTH, -1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 今天凌晨时间戳 秒
     * 
     * @return
     * @throws Exception
     */
    public static Long getToday() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 明日凌晨时间戳 秒
     * 
     * @return
     * @throws Exception
     */
    public static Long getTomorrow() {
        return getToday() + 86400;
    }

    /**
     * 计算time2比time1多的天数
     * 
     * @param time1
     * @param time2
     * @return
     */
    public static int calculateDifferDays(Integer time1, Integer time2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(new Date(time1 * 1000L));
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(new Date(time2 * 1000L));
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) // 同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                {
                    timeDistance += 366;
                } else // 不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else // 不同年
        {
            return day2 - day1;
        }
    }

    public static Integer activityEndTime(Long startTime, Long endTime, String weekDay, String time) {
        Long timeStap = System.currentTimeMillis();
        Long activityEndTime = 0L;
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(timeStap));
        String weekDayNow = (cal.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? "7" : (cal.get(Calendar.DAY_OF_WEEK) - 1) + "";
        String hourNow = cal.get(Calendar.HOUR_OF_DAY) + "";
        if ((timeStap >= startTime || timeStap <= endTime) && weekDay.indexOf(weekDayNow) > -1
            && time.indexOf(hourNow) > -1) {
            String[] timeArray = time.split(",");
            int baseHour = Integer.parseInt(hourNow);
            for (int i = 0; i < timeArray.length; i++) {
                if (hourNow.equals(timeArray[i])) {
                    for (int y = 0; y < timeArray.length - i; y++) {
                        if (baseHour + 1 == Integer.parseInt(timeArray[i + y])) {
                            baseHour++;
                        }
                    }
                }
            }
            cal.set(Calendar.HOUR_OF_DAY, baseHour);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            activityEndTime = cal.getTimeInMillis() / 1000;
            return Integer.parseInt(activityEndTime.toString());
        } else {
            return Integer.parseInt((endTime / 1000) + "");
        }
    }

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String formatDate(Date date) {
        return format(date, DATE_PATTERN_TIME);
    }

    /**
     * 时间戳转换 yyyy-MM-dd HH:mm 日期
     * 
     * @param date
     * @return
     */
    public static String formatDate(Integer date) {
        Date d = new Date(date * 1000L);
        return format(d, DATE_PATTERN_TIME);
    }

    /**
     *
     * @param date
     * @param formate
     * @return
     */
    public static String formatDate(Integer date, String formate) {
        Date d = new Date(date * 1000L);
        return format(d, formate);
    }

    public static String formatSec(Date date) {
        return format(date, DATE_TIME_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static String formate(Integer seconds, String pattern) {
        return format(parseFromSeconds(seconds), pattern);
    }

    public static Date parseFromSeconds(Integer seconds) {
        if (seconds != null && seconds != 0) {
            return new Date(Long.valueOf(seconds + "000"));
        }
        return null;
    }

    /**
     * 获得开始时间
     * 
     * @param calendar
     * @return
     */
    public static Long getBeginTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获得结束时间
     * 
     * @param calendar
     * @return
     */
    public static Long getEndTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取昨天当前时刻
     * 
     * @return
     */
    public static Calendar getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar;
    }

    /**
     * 获取昨天开始时间
     * 
     * @return
     */
    public static Long getYesterdayBegin() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取昨天结束时间
     * 
     * @return
     */
    public static Long getYesterdayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 增加日期
     * 
     * @param c
     * @param day 天数
     * @return
     */
    public static Calendar addDay(Calendar c, int day) {
        c.add(Calendar.DAY_OF_MONTH, day);
        return c;
    }

    /**
     * 获取某年月，第一天开始时间
     * 
     * @param year
     * @param month
     * @return
     */
    public static Long getFirstOfMonth(Integer year, Integer month) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 获取某年月，最后一天结束时间
     * 
     * @param year
     * @param month
     * @return
     */
    public static Long getLastOfMonth(Integer year, Integer month) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 日期字符串转化日期对象，默认格式yyyy-MM-dd HH:mm:ss
     * 
     * @param dateStr
     * @return
     * @throws Exception
     */
    public static Date parseDate(String dateStr) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(dateStr)) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.parse(dateStr);
    }

    /**
     * 日期字符串转化日期对象
     * 
     * @param dateStr
     * @param pattern
     * @return
     * @throws Exception
     */
    public static Date parseDate(String dateStr, String pattern) throws Exception {
        if (StringUtils.isEmpty(dateStr) || StringUtils.isEmpty(pattern)) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.parse(dateStr);
    }

    /**
     * 时间转int
     * 
     * @param dateStr
     * @return
     */
    public static Integer pareStrDateToInt(String dateStr) {
        Integer time = 0;
        try {
            time = Integer.parseInt(DateUtils.parseDate(dateStr, DateUtils.DATE_PATTERN).getTime() / 1000 + "");
        } catch (Exception e) {

        }
        return time;
    }

    /**
     * 日期加指定年
     * 
     * @param date
     * @param year
     * @return
     */
    public static Long addYear(Date date, Integer year) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setTime(date);
        c.add(Calendar.YEAR, year);
        return c.getTimeInMillis() / 1000;
    }

    /**
     * 比较两个时间戳是否是同年同月同日
     *
     * @param target1
     * @param target2
     * @return
     * @throws Exception
     */
    public static boolean compareYMD(Integer target1, Integer target2) {
        if (target1 == null || target2 == null) {
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String target1Str = sdf.format(new Date(target1 * 1000L));
        String target2Str = sdf.format(new Date(target2 * 1000L));
        return target1Str.equals(target2Str);
    }

    public static Integer getYesterdayTime() {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DATE, -1);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        Integer endTime = Integer.parseInt((calendar2.getTimeInMillis() / 1000) + "");
        return endTime;
    }

    /**
     * 获取两个日期之间的所有日期
     * 
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 字符串时间根据格式转换int类型时间戳
     *
     * @param date
     * @param pattern
     * @return
     */
    public static Integer getTimestampByFormat(String date, String pattern) {
        Integer timestamp = null;
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(date)) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            try {
                timestamp = Math.toIntExact(df.parse(date).getTime() / 1000);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return timestamp;
    }

    /**
     * 获取到指定日期
     * 
     * @param date
     * @return
     */
    public static Date getDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date newDate = null;
        try {
            newDate = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * 获取到指定日期
     * 
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date getDate(String dateStr, String pattern) {
        if (dateStr == null || dateStr == "") {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date newDate = null;
        try {
            newDate = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * 字符串时间根据格式转换long类型时间戳
     *
     * @param date
     * @param pattern
     * @return
     */
    public static long getTimesByFormat(String date, String pattern) {
        Long timestamp = null;
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(date)) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            try {
                timestamp = df.parse(date).getTime() / 1000;

            } catch (ParseException e) {
            }
        }
        return timestamp;
    }

    /**
     * 获取当前秒的时间戳
     *
     * @return
     */
    public static Integer getTimeStampByDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Long time = cal.getTimeInMillis() / 1000;
        return time.intValue();
    }
}